package henu.graduation_design.onlineshop;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;

import henu.graduation_design.adapter.ProductListAdapter;
import henu.graduation_design.tool.AnalyseProductJson;
import henu.graduation_design.util.JsonLoader;
import henu.graduation_design.view.XListView;
import henu.graduation_design.view.XListView.IXListViewListener;
import android.os.Bundle;
import android.os.Handler;
import android.view.Window;
import android.widget.Toast;
import android.app.Activity;
import android.content.Intent;

public class ProductListActivity extends Activity implements IXListViewListener {

	private XListView mListView;
	private Handler mHandler;

	ProductListAdapter adapter;
	static List<HashMap<String, Object>> list;
	static List<HashMap<String, Object>> attachList;
	private String categoryID;
	private String productID;

	private JsonLoader jsonLoader = new JsonLoader();

	private void loadJson(final String url) {
		jsonLoader.getJsonArray(url, new JsonLoader.JsonArrayCallback() {
			public void jsonLoaded(JSONArray jsonArray) {
				list.clear();
				AnalyseProductJson analyse = new AnalyseProductJson(jsonArray);
				try {
					list.addAll(analyse.getList());
					if (list.size() != 0) {
						productID = list.get(list.size() - 1).get("id")
								.toString();
					}
				} catch (JSONException e) {
					Toast.makeText(getApplicationContext(),
							"解析category的json数据发生异常", Toast.LENGTH_LONG).show();
				}
				adapter = new ProductListAdapter(ProductListActivity.this, list);
				mListView.setAdapter(adapter);
			}
		});
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_product_list);
		Intent intent = getIntent();// 获得传过来的Intent。
		categoryID = intent.getStringExtra("ID");// 根据键name取出�怠�
		list = new ArrayList<HashMap<String, Object>>();
		attachList = new ArrayList<HashMap<String, Object>>();

		mListView = (XListView) findViewById(R.id.list);
		mListView.setPullLoadEnable(true);

		try {
			String urlString = getString(R.string.url_productlist)
					+ "?categoryid=" + categoryID + "&productid=-1";
			loadJson(urlString);
		} catch (Exception e) {
			e.printStackTrace();
		}

		mListView.setXListViewListener(this);
		mHandler = new Handler();

	}

	private void onLoad() {
		mListView.stopRefresh();
		mListView.stopLoadMore();
		mListView.setRefreshTime("刚刚");
	}

	@Override
	public void onRefresh() {
		mHandler.postDelayed(new Runnable() {
			@Override
			public void run() {
				try {
					String urlString = getString(R.string.url_productlist)
							+ "?categoryid=" + categoryID + "&productid=-1";
					loadJson(urlString);
				} catch (Exception e) {
					e.printStackTrace();
				}
				onLoad();
			}
		}, 2000);
	}

	@Override
	public void onLoadMore() {
		mHandler.postDelayed(new Runnable() {
			@Override
			public void run() {
				String urlString = getString(R.string.url_productlist)
						+ "?categoryid=" + categoryID + "&productid="
						+ productID;
				jsonLoader.getJsonArray(urlString,
						new JsonLoader.JsonArrayCallback() {
							public void jsonLoaded(JSONArray jsonArray) {
								attachList.clear();
								AnalyseProductJson analyse = new AnalyseProductJson(
										jsonArray);
								try {
									attachList = analyse.getList();
									if (attachList.size() != 0) {
										productID = attachList
												.get(attachList.size() - 1)
												.get("id").toString();
									}
								} catch (JSONException e) {
									Toast.makeText(getApplicationContext(),
											"解析category的json数据发生异常",
											Toast.LENGTH_LONG).show();
								}
								list.addAll(attachList);
								adapter.notifyDataSetChanged();
							}
						});
				onLoad();
			}
		}, 2000);
	}
}
